"""
交易 - 退款相关模型
"""

from typing import Dict, Any, Optional
from jlpay.model.base import OrgBaseReq, BaseResponse


class RefundsRequest(OrgBaseReq):
    """退款申请请求"""
    
    API_URL = "/open/trans/refunds"
    
    def __init__(self):
        """初始化退款申请请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.total_fee = None  # 交易金额
        self.remark = None  # 备注
        self.mch_create_ip = None  # 终端IP
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
        self.notify_url = None  # 推送地址
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def set_ori_out_trade_no(self, ori_out_trade_no: str):
        """设置原外部订单号"""
        self.ori_out_trade_no = ori_out_trade_no
        return self
    
    def set_ori_transaction_id(self, ori_transaction_id: str):
        """设置原平台订单号"""
        self.ori_transaction_id = ori_transaction_id
        return self
    
    def set_total_fee(self, total_fee: str):
        """设置交易金额"""
        self.total_fee = total_fee
        return self
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
        return self
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
        return self
    
    def set_notify_url(self, notify_url: str):
        """设置推送地址"""
        self.notify_url = notify_url
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'total_fee': self.total_fee,
            'remark': self.remark,
            'mch_create_ip': self.mch_create_ip,
            'longitude': self.longitude,
            'latitude': self.latitude,
            'notify_url': self.notify_url
        })
        return data


class RefundsResponse(BaseResponse):
    """退款申请响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化退款申请响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 1：待确认 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.total_fee = None  # 交易金额
        self.pay_type = None  # 交易类型
        self.trans_time = None  # 交易时间
        self.success_time = None  # 退款完成时间
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.ori_out_trade_no = response_dict.get('ori_out_trade_no')
            self.ori_transaction_id = response_dict.get('ori_transaction_id')
            self.total_fee = response_dict.get('total_fee')
            self.pay_type = response_dict.get('pay_type')
            self.trans_time = response_dict.get('trans_time')
            self.success_time = response_dict.get('success_time')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'total_fee': self.total_fee,
            'pay_type': self.pay_type,
            'trans_time': self.trans_time,
            'success_time': self.success_time
        })
        return data 